package life.kenny.community.controller;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import life.kenny.community.cache.TagCache;
import life.kenny.community.dto.GiteeUser;
import life.kenny.community.dto.QuestionDTO;
import life.kenny.community.model.Question;
import life.kenny.community.model.User;
import life.kenny.community.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.jni.Mmap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Jay
 * @date 2021/9/17 3:35 下午
 */
@Controller
public class PublishController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/publish/{id}")
    public String edit(@PathVariable("id") Long id,
                       Model model) {
        QuestionDTO byQuestionId = questionService.getByQuestionId(id);
        model.addAttribute("title", byQuestionId.getTitle());
        model.addAttribute("description", byQuestionId.getDescription());
        model.addAttribute("tag", byQuestionId.getTag());
        model.addAttribute("id", byQuestionId.getId());
        return "publish";
    }

    @GetMapping("/publish")
    public String publish(Model model) {
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(value = "title",required = false)String title,
            @RequestParam(value = "description",required = false)String description,
            @RequestParam(value = "tag",required = false)String tag,
            @RequestParam(value = "id",required = false)Long id,
            HttpServletRequest request,
            Model model){
        model.addAttribute("title", title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);
        //非空判断
        if (StringUtils.isBlank(title)) {
            model.addAttribute("error", "标题不能为空");
            return "publish";
        }
        if (StringUtils.length(title) > 50) {
            model.addAttribute("error", "标题最多50个字符");
            return "publish";
        }
        if (StringUtils.isBlank(description)) {
            model.addAttribute("error", "问题补充不能为空");
            return "publish";
        }
        if (StringUtils.isBlank(tag)) {
            model.addAttribute("error", "标签不能为空");
            return "publish";
        }
        //获取用户
        //GiteeUser user = (GiteeUser) request.getSession().getAttribute("user");
        Object user = request.getSession().getAttribute("user");
        User gUser = new User();
        BeanUtils.copyProperties(user, gUser);
        if (user == null) {
            model.addAttribute("error", "用户未登陆");
            return "publish";
        }
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(Long.valueOf(gUser.getAccountId()));
        question.setId(id);
        questionService.createOrUpdate(question);
        return "redirect:/";
    }
}
